package com.yumijinfu.activity;

import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.lzy.okhttputils.model.HttpParams;
import com.yumijinfu.R;
import com.yumijinfu.base.BaseActivity;
import com.yumijinfu.http.HttpCallBack;
import com.yumijinfu.http.HttpUtils;
import com.yumijinfu.mian.Config;
import com.yumijinfu.tool.CodeUtils;
import com.yumijinfu.tool.MyCountTimer;
import com.yumijinfu.tool.VibratorUtil;
import com.yumijinfu.util.DES;
import com.yumijinfu.view.ClearEditText;
import com.yumijinfu.view.TitleBar;

import org.json.JSONObject;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 注册有分两步
 */
public class RegisteredSecondActivity extends BaseActivity {

    @Bind(R.id.titlebar)
    TitleBar titlebar;
    @Bind(R.id.et_register_phone)
    ClearEditText etRegisterPhone;
    @Bind(R.id.et_register_tuxing_code)
    ClearEditText etRegisterTuxingCode;
    @Bind(R.id.et_register_message_code)
    ClearEditText etRegisterMessageCode;
    @Bind(R.id.iv_code)
    ImageView ivCode;
    @Bind(R.id.tv_yanzhengma_code)
    TextView tvYanzhengmaCode;


    //
    private String codeStr;
    private CodeUtils codeUtils;

    //
    private MyCountTimer myCountTimer;
    //
    private String registerPhone;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        setContentView(R.layout.activity_registered_second);
        ButterKnife.bind(this);
        super.onCreate(savedInstanceState);
    }

    @Override
    protected void initTitle() {
        codeUtils = CodeUtils.getInstance();

        titlebar.setTitleBar(R.drawable.back_selector, "注册", 0);
        titlebar.setTitleBarBackgroundColor(getResources().getColor(R.color.tou_ming));
    }

    @Override
    protected void initView() {

        Bitmap bitmap = codeUtils.createBitmap();
        ivCode.setImageBitmap(bitmap);
    }

    @Override
    protected void setData() {

    }

    @OnClick({R.id.titlebar_iv_back, R.id.tv_next_register, R.id.tv_yanzhengma_code, R.id.iv_code})
    @Override
    public void widgetClick(View v) {

        switch (v.getId()) {
            case R.id.titlebar_iv_back:


                finish();


                break;


            case R.id.tv_next_register:    //注册第一步
                long[] pattern = {100, 400, 100, 400};
                registerPhone = etRegisterPhone.getText().toString().trim();
                String registerTuxingCode = etRegisterTuxingCode.getText().toString().trim();
                String registerMessageCode = etRegisterMessageCode.getText().toString().trim();
                //获取图形验证码
                codeStr = codeUtils.getCode();

                if (TextUtils.isEmpty(registerPhone)) {
                    etRegisterPhone.setShakeAnimation();
                    VibratorUtil.Vibrate(RegisteredSecondActivity.this, pattern, false);
                    Toast.makeText(RegisteredSecondActivity.this, "手机号不能为空", Toast.LENGTH_SHORT).show();
                    return;
                } else if (TextUtils.isEmpty(registerTuxingCode)) {
                    etRegisterTuxingCode.setShakeAnimation();
                    VibratorUtil.Vibrate(RegisteredSecondActivity.this, pattern, false);
                    Toast.makeText(RegisteredSecondActivity.this, "图形验证码不能为空", Toast.LENGTH_SHORT).show();
                    return;

                } else if (!codeStr.equals(registerTuxingCode)) {
                    etRegisterTuxingCode.setShakeAnimation();
                    VibratorUtil.Vibrate(RegisteredSecondActivity.this, pattern, false);
                    Toast.makeText(RegisteredSecondActivity.this, "图形验证码输入错误", Toast.LENGTH_SHORT).show();
                    return;

                } else if (TextUtils.isEmpty(registerMessageCode)) {
                    etRegisterMessageCode.setShakeAnimation();
                    VibratorUtil.Vibrate(RegisteredSecondActivity.this, pattern, false);
                    Toast.makeText(RegisteredSecondActivity.this, "短信验证码不能空", Toast.LENGTH_SHORT).show();
                    return;
                } else {

                    // 注册第二步 判断验证码是否可以通过

                    try {
                        HttpParams params = new HttpParams();
                        params.put("phoneCode", DES.encryptDES(etRegisterMessageCode.getText().toString().trim(), Config.DES_KEY));
                        params.put("userAccount", etRegisterPhone.getText().toString().trim());
                        HttpUtils.post(Config.REGIST_TWO, params, new HttpCallBack(this, "加载中...") {
                            @Override
                            public void onSucceed(JSONObject result, String code, String message) {
                                Toast.makeText(RegisteredSecondActivity.this, message, Toast.LENGTH_SHORT).show();
                                Intent intent = new Intent(RegisteredSecondActivity.this, RegisterActivity.class);
                                intent.putExtra("phoneCode", etRegisterMessageCode.getText().toString().trim());
                                intent.putExtra("userAccount", etRegisterPhone.getText().toString().trim());
////                          // 传递相关参数
                                startActivity(intent);
                                finish();
                            }

                            @Override
                            public void onError(String code, String message) {
                                Toast.makeText(RegisteredSecondActivity.this, message, Toast.LENGTH_SHORT).show();
                            }
                        });
                    } catch (Exception e) {
                        e.printStackTrace();
                    }


                }

                break;

            case R.id.tv_yanzhengma_code:   //短信验证码
                registerPhone = etRegisterPhone.getText().toString().trim();
//                Toast.makeText(RegisteredSecondActivity.this, "验证码已发送,请注意查收", Toast.LENGTH_SHORT).show();

                //手机号码不能为空
                if (TextUtils.isEmpty(registerPhone)) {
                    etRegisterPhone.setShakeAnimation();
                    Toast.makeText(RegisteredSecondActivity.this, "手机号不能为空", Toast.LENGTH_SHORT).show();
                    return;

                } else {

                    //调用获取验证接口
                    myCountTimer = new MyCountTimer(60000, 1000, tvYanzhengmaCode, R.string.txt_getMsgCode_validate);
                    myCountTimer.start();
                    //
                    HttpParams params = new HttpParams();
                    params.put("userAccount", registerPhone);
                    HttpUtils.post(Config.REGIST_ONE, params, new HttpCallBack(this, "验证码发送中") {
                        @Override
                        public void onSucceed(JSONObject result, String code, String message) {
                            Toast.makeText(RegisteredSecondActivity.this, message, Toast.LENGTH_SHORT).show();


                        }

                        @Override
                        public void onError(String code, String message) {
                            Toast.makeText(RegisteredSecondActivity.this, message, Toast.LENGTH_SHORT).show();
                        }
                    });


                }


                break;

            case R.id.iv_code:   //切换图形验证码

                ivCode.setImageBitmap(codeUtils.createBitmap());

                break;


            default:
        }

    }
}
